<template>
    <div>
        <div class="pdf-file">
            <pdf
                ref="printConent"
                v-for="i in numPages"
                :key="i"
                :src="pdfpath"
                :page="i"
            >
            </pdf>
        </div>
    </div>
</template>

<script>
import pdf from 'vue-pdf';
import {
    fileDownload
} from '@/api/shenbao';
    export default {
        name: 'PdfFile',
        data() {
            return {
                numPages: '',
                path: '',
                cxj_cxj_id: '',
                pdfpath: '',
            }
        },
        components: { pdf },
        methods: {
            get_download() {
                let that = this;
                fileDownload({
                    cxj_cxj_id: this.cxj_cxj_id,
                    path: this.path
                }).then(res=>{
                    console.log(res, 'res')
                    if (res.data) {
                        that.pdfpath = that.getObjectURL(res.data);
                        // console.log(that.pdfpath)
                        const loadingTask = pdf.createLoadingTask(that.pdfpath)
                        loadingTask.promise.then(load => {
                            that.numPages = load.numPages
                            // console.log(that.numPages, 'that.numPages')
                        }).catch(err => {
                            // console.log(err)
                        })
                    }
                })
            },
            getObjectURL(file) {
                let url = null
                if (window.createObjectURL !== undefined) { // basic
                    url = window.createObjectURL(file)
                } else if (window.webkitURL !== undefined) { // webkit or chrome
                    try {
                        url = window.webkitURL.createObjectURL(file)
                    } catch (error) {
                        // console.log(error)
                    }
                } else if (window.URL !== undefined) { // mozilla(firefox)
                    try {
                        url = window.URL.createObjectURL(file)
                    } catch (error) {
                        // console.log(error)
                    }
                }
                return url;
            }
        },
        mounted() {
            this.cxj_cxj_id = this.$route.query.cxj_cxj_id;
            this.path = this.$route.query.path;
            console.log('232323',this.path)
            this.get_download();
        }
    }
</script>

<style lang="less" scoped>
</style>
